﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

using Common.Base;

namespace GameCommon.Base
{
    public class SpriteCollection : IEnumerable<Sprite>
    {
        private IList<Sprite> sprites;

        private readonly Size screenSize;

        public SpriteCollection()
        {
            this.sprites = new List<Sprite>();
            this.screenSize = new Size();
        }

        public Size ScreenSize
        {
            get

            {
                return this.screenSize;
            }
        }

        IEnumerator<Sprite> IEnumerable<Sprite>.GetEnumerator()
        {
            return this.sprites.GetEnumerator();
        }

        public IEnumerator GetEnumerator()
        {
            return this.sprites.GetEnumerator();
        }

        public void Add(Sprite sprite)
        {
            var width = sprite.DestinationRectangle.X + sprite.DestinationRectangle.Width;
            if (width > screenSize.Width)
            {
                screenSize.Width = width;
            }

            var height = sprite.DestinationRectangle.Y + sprite.DestinationRectangle.Height;
            if (height > screenSize.Height)
            {
                screenSize.Height = height;
            }

            this.sprites.Add(sprite);
        }

        public void Order()
        {
            this.sprites = this.sprites.OrderBy(x => x.Layer).ToList();
        }

        public void Clear()
        {
            this.sprites.Clear();
        }
    }
}